package com.hzncc.scaffold.controller.Front.Job;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.JobListBo;
import com.hzncc.scaffold.entity.Other.Cvpost;
import com.hzncc.scaffold.entity.Other.Job;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.JobMapper;
import com.hzncc.scaffold.service.CvpostService;
import com.hzncc.scaffold.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-15 11:21
 * @Description: null
 */
@RequestMapping("/job")
@Api(tags = "前台接口-人才需求")
@RestController
public class JobController {

    @Autowired
    JobMapper jobMapper;

    @Autowired
    CvpostService cvpostService;

    @Autowired
    JobService jobService;

    @GetMapping("/list")
    @ApiOperation("人才需求列表")
    public ResultT list(Integer pageNum, Integer pageSize) {

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<JobListBo> jobListBos = jobMapper.list();
        ArrayList<FrontJobListVo> frontJobListVos = new ArrayList<>();

        for (JobListBo jobListBo : jobListBos) {
            FrontJobListVo frontJobListVo = BeanUtil.toBean(jobListBo, FrontJobListVo.class);

            Institute institute = new Institute();
            institute.setId(jobListBo.getInstituteId());
            institute.setName(jobListBo.getInstituteName());
            institute.setRole(jobListBo.getInstituteRole());

            frontJobListVo.setInstitute(institute);

            frontJobListVos.add(frontJobListVo);
        }

//        PageInfo<FrontJobListVo> info = new PageInfo<>();
//        info.setList(frontJobListVos);
//        info.setTotal(new PageInfo<>(jobListBos).getTotal());

        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",frontJobListVos);
        map.put("count",new PageInfo<>(jobListBos).getTotal());

        return ResultT.success(map);
    }

    @PostMapping("/postCv")
    @ApiOperation("投递简历")
    public ResultT postCv(@RequestBody PostCvVo postCvVo) {
        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }
        Integer jobId = postCvVo.getJobId();
        String title = postCvVo.getTitle();
        String url = postCvVo.getUrl();

        Cvpost cvpost = new Cvpost();
        cvpost.setJobId(jobId);
        cvpost.setTitle(title);
        cvpost.setUrl(url);
        cvpost.setUserId(StpUtil.getLoginIdAsInt());

        boolean save = cvpostService.save(cvpost);

        if (save) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }


    @PostMapping("/checkPosted")
    @ApiOperation("检查简历是否投递")
    public ResultT checkPosted(@RequestBody JobIdVo jobIdVo) {

        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }

        Integer jobId = jobIdVo.getJobId();

        LambdaQueryWrapper<Cvpost> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Cvpost::getUserId, StpUtil.getLoginIdAsInt());
        lqw.eq(Cvpost::getJobId, jobId);

        List<Cvpost> cvpostList = cvpostService.list(lqw);

        if (CollUtil.isNotEmpty(cvpostList)) {
            return ResultT.fail("已投递过该职位，请勿重复投递");
        } else {
            return ResultT.success("操作成功");
        }
    }

    @PostMapping("/siteApplyMultiFiles")
    @ApiOperation("站点申报申请")
    public ResultT siteApplyMultiFiles() {
        return null;
    }

    @PostMapping("/siteApplyStatus")
    @ApiOperation("站点申报状态")
    public ResultT siteApplyStatus() {
        return null;
    }

    @GetMapping("/jobList")
    @ApiOperation("人才需求列表")
    public ResultT jobList(Integer pageNum, Integer pageSize, Integer status, String keyword) {
        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }
        QueryWrapper<InstituteCenterJobListVo> qw = new QueryWrapper<>();
        qw.eq("job.belongsTo", StpUtil.getLoginIdAsInt());
        if (status != null) {
            qw.eq("job.status", status);
        }
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("job.title", keyword);
        }
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }

        List<InstituteCenterJobListVo> instituteCenterJobListVos = jobMapper.instituteJobList(qw);


        for (InstituteCenterJobListVo instituteCenterJobListVo : instituteCenterJobListVos) {
            LambdaQueryWrapper<Cvpost> lqww = new LambdaQueryWrapper<>();
            lqww.eq(Cvpost::getJobId, instituteCenterJobListVo.getId());

            List<Integer> cvpostIdList = cvpostService.list(lqww).stream().map(cvpost -> cvpost.getId()).collect(Collectors.toList());
            instituteCenterJobListVo.setCvposts(cvpostIdList);
        }

        return ResultT.success(new PageInfo<>(instituteCenterJobListVos));
    }

}
